package com.kma.summer2012.client.Header;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.Autorisation.Authorisation;
import com.kma.summer2012.client.Autorisation.CrumbsAuthorisationService;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.shared.model.UICrumbsUser;

public class Header  extends Composite {
    interface CurrentUserUiBinder extends UiBinder<Widget, Header> {
    }

    private static CurrentUserUiBinder ourUiBinder = GWT.create(CurrentUserUiBinder.class);

    @UiField
    protected Label nameUser;

    @UiField
    protected Label positionUser;

    @UiField
    protected Anchor home;

    @UiField
    protected Button logOut;

    public Header(UICrumbsUser user) {
        HTMLPanel rootElement = (HTMLPanel)ourUiBinder.createAndBindUi(this);
        initWidget(rootElement);
        this.nameUser.setText(user.getSurnmNmSecondnm());
        this.positionUser.setText(user.getPosition());
        this.home.setText("Home");
        this.logOut.setText("Logout");
        CrumbsEventBus.getInstance().addHandler(LogoutEvent.getType(), new LogoutEvent.LogoutEventHandler() {
            @Override
            public void onLogout() {
                RootPanel.get().remove(Header.this);
                RootPanel.get().clear();
                RootPanel.get().add(new Authorisation());
            }
        });
        this.logOut.addClickHandler(new ClickHandler(){
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new LogoutEvent());
            }
        });
    }
}